package com.xxwy.hard.taskqueue;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.ScheduledFuture;

import java.util.concurrent.TimeUnit;

/**
 * @author wy
 * @date 2020/3/5 10:10
 */
public class ScheduleHandler extends ChannelInboundHandlerAdapter {

  /**
   * 一个定时任务 ， scheduleQueue中
   * @param ctx
   * @param msg
   * @throws Exception
   */
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ScheduledFuture<String> schedule = ctx.channel().eventLoop().schedule(() -> {
      ctx.channel().writeAndFlush(Unpooled.copiedBuffer("hi schedule task 5s", CharsetUtil.UTF_8));
      return "hi";
    }, 5, TimeUnit.SECONDS);

  }
}
